define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'post/post/index' + location.search,
                    add_url: 'post/post/add',
                    // edit_url: 'post/post/edit',
                    del_url: 'post/post/del',
                    // multi_url: 'post/post/multi',
                    import_url: 'post/post/import',
                    table: 'post_post',
                }
            });

            var table = $("#table");
            // 初始化表格后添加点击事件
            table.on('post-body.bs.table', function () {
                $(".order-detail").on('click', function () {
                    var id = $(this).data('id');
                    // 这里复用操作列中的详情按钮逻辑
                    Fast.api.open('user/user/detail/ids/' + id, __('详情'), {
                        callback: function (data) {
                            Layer.alert("接收到回传数据：" + JSON.stringify(data), { title: "回传数据" });
                        },
                        area: ['80%', '80%']
                    });
                });
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                // sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        // {field: 'auth_id', title: __('Auth_id')},
                        // {field: 'type_id', title: __('Type_id')},
                        {
                            field: 'user.nickname', title: __('用户'), formatter: function (value, row, index) {
                                let html = `<img src="${row.user.avatar}" style="width:30px;height:30px;border-radius:50%;margin-right:5px;">
                                <a href="javascript:;" class="order-detail" data-id="${row.user.id}">${row.user.nickname}</a>`;
                                return html;
                            }
                        },
                        // { field: 'type.name', title: __('Type.name'), operate: 'LIKE' },
                        { field: 'province_city', title: __('Province_city'), operate: 'LIKE', visible: false },
                        { field: 'urban_city', title: __('Urban_city'), operate: 'LIKE', visible: false },
                        { field: 'district_city', title: __('District_city'), operate: 'LIKE', visible: false },
                        { field: 'address', title: __('所在区域'), operate: false },
                        { field: 'name', title: __('Name'), operate: 'LIKE' },
                        { field: 'mobile', title: __('联系电话'), operate: 'LIKE' },
                        { field: 'wx_image', title: __('Wx_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'content', title: __('Content'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        // { field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images },
                        { field: 'stutes', title: __('审核状态'), searchList: { "0": __('未审核'), "1": __('已审核'), "2": __('审核失败') }, formatter: Table.api.formatter.status },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        // { field: 'weigh', title: __('Weigh'), operate: false },
                        {
                            field: 'operate', title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'handle',
                                    text: __('审核'),
                                    title: __('审核'),
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-check',
                                    url: 'post/shenpost/handle',
                                    callback: function (data) {
                                        Layer.alert(data.msg);
                                        // 刷新表格
                                        table.bootstrapTable('refresh');
                                    }
                                },
                                {
                                    name: 'detail',
                                    text: __('详情'),
                                    title: __('详情'),
                                    classname: 'btn btn-xs btn-primary btn-dialog btn-detail',
                                    icon: 'fa fa-list',
                                    url: 'post/post/detail',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), { title: "回传数据" });
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'post/post/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'name', title: __('Name'), align: 'left' },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'post/post/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'post/post/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        handle: function () {
            $(function () {
                // 初始显示/隐藏原因框
                toggleReasonBox();

                // 监听审核状态变化
                $('#audit-status').change(function () {
                    toggleReasonBox();
                });

                function toggleReasonBox() {
                    if ($('#audit-status').val() == '2') {
                        $('#reason-group').show();
                    } else {
                        $('#reason-group').hide();
                    }
                }

                // 表单提交处理
                $('#audit-form').submit(function (e) {
                    e.preventDefault();
                    var form = $(this);
                    Fast.api.ajax({
                        url: form.attr('action'),
                        type: 'post',
                        data: form.serialize(),
                        success: function (data) {
                            Layer.alert(data.msg, function () {
                                // 关闭当前弹窗并刷新表格
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                parent.$('#table').bootstrapTable('refresh');
                            });
                        }
                    });
                    return false;
                });
            });
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
